package com.zjff.myshop.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2的接口配置
 * 文档浏览器地址：http:127.0.0.1:5050/doc.html
 */
@Configuration
@EnableSwagger2WebMvc
@Profile({"dev", "test"}) //配置该bean用于开发、测试环境
public class SwaggerConfiguration {
    @Bean(value = "dockerBean")
    public Docket dockerBean() {
        String groupName = "v1";
        return new Docket(DocumentationType.SWAGGER_2)    //SWAGGER_2
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.zjff.myshop.front.controller"))
//                .apis(RequestHandlerSelectors.basePackage("com.zjff.myshop.admin.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(globalOperationParameters())
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessageList())
                .globalResponseMessage(RequestMethod.POST, responseMessageList())
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList());
    }

    //添加摘要信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("标题：我的商城接口文档")
                .description("描述：基于swagger knife4j自动创建的接口文档，用于管理我的商城信息,具体包括用户,商品等模块...")
                .termsOfServiceUrl("http://swagger.io/")
                .contact(new Contact("watchping", "127.0.0.1", "watchping@163.com"))
                .version("0.0.1")
                .build();
    }

    // 设置全局参数
    private List<Parameter> globalOperationParameters() {
        // 添加token参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder
                .parameterType("header")
                .name("token")
                .description("用户token")
                .modelRef(new ModelRef("string"))
                .required(false).build();
        List<Parameter> parameterList = new ArrayList<Parameter>();
        parameterList.add(parameterBuilder.build());
        return parameterList;
    }

    // 自定义响应状态
    private List<ResponseMessage> responseMessageList() {

        ResponseMessage responseMessage500 = new ResponseMessageBuilder()
                .code(500)
                .message("服务器发生异常")
                .build();
        ResponseMessage responseMessage401 = new ResponseMessageBuilder()
                .code(401)
                .message("未登录")
                .build();
        ResponseMessage responseMessage403 = new ResponseMessageBuilder()
                .code(403)
                .message("资源不可用")
                .build();
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(responseMessage500);
        responseMessageList.add(responseMessage401);
        responseMessageList.add(responseMessage403);
        return responseMessageList;
    }
}